/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopLuckyBurst;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopLuckyBurstDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopLuckyBurstProduct;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopLuckyBurstProductDao;

/**
 * 乐购幸运爆款Service
 * @author 罗天文
 * @version 2018-05-15
 */
@Service
@Transactional(readOnly = true)
public class HkShopLuckyBurstService extends CrudService<HkShopLuckyBurstDao, HkShopLuckyBurst> {

	@Autowired
	private HkShopLuckyBurstProductDao hkShopLuckyBurstProductDao;
	
	public HkShopLuckyBurst get(String id) {
		HkShopLuckyBurst hkShopLuckyBurst = super.get(id);
		hkShopLuckyBurst.setHkShopLuckyBurstProductList(hkShopLuckyBurstProductDao.findList(new HkShopLuckyBurstProduct(hkShopLuckyBurst)));
		return hkShopLuckyBurst;
	}
	
	public List<HkShopLuckyBurst> findList(HkShopLuckyBurst hkShopLuckyBurst) {
		return super.findList(hkShopLuckyBurst);
	}
	
	public Page<HkShopLuckyBurst> findPage(Page<HkShopLuckyBurst> page, HkShopLuckyBurst hkShopLuckyBurst) {
		return super.findPage(page, hkShopLuckyBurst);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopLuckyBurst hkShopLuckyBurst) {
		super.save(hkShopLuckyBurst);
		for (HkShopLuckyBurstProduct hkShopLuckyBurstProduct : hkShopLuckyBurst.getHkShopLuckyBurstProductList()){
			if (hkShopLuckyBurstProduct.getId() == null){
				continue;
			}
			hkShopLuckyBurstProduct.setActivityType("1");
			hkShopLuckyBurstProduct.setBeginTime(hkShopLuckyBurst.getBeginTime());
			hkShopLuckyBurstProduct.setEndTime(hkShopLuckyBurst.getEndTime());
			if (HkShopLuckyBurstProduct.DEL_FLAG_NORMAL.equals(hkShopLuckyBurstProduct.getDelFlag())){
				if (StringUtils.isBlank(hkShopLuckyBurstProduct.getId())){
					hkShopLuckyBurstProduct.setBurst(hkShopLuckyBurst);
					hkShopLuckyBurstProduct.preInsert();
					hkShopLuckyBurstProductDao.insert(hkShopLuckyBurstProduct);
				}else{
					hkShopLuckyBurstProduct.preUpdate();
					hkShopLuckyBurstProductDao.update(hkShopLuckyBurstProduct);
				}
			}else{
				hkShopLuckyBurstProductDao.delete(hkShopLuckyBurstProduct);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopLuckyBurst hkShopLuckyBurst) {
		super.delete(hkShopLuckyBurst);
		hkShopLuckyBurstProductDao.delete(new HkShopLuckyBurstProduct(hkShopLuckyBurst));
	}
	
}